package reduce.fz.com.reduce.activity;

import android.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.format.DateUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.util.HashMap;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.StoryReply;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.LoadingDialog;
import reduce.fz.com.reduce.fragment.StoryReplyFragment;
import reduce.fz.com.reduce.service.StoryService;
import reduce.fz.com.reduce.service.preference.UserPreference;
import reduce.fz.com.reduce.util.DateUtil;
import reduce.fz.com.reduce.util.InputMethodUtil;
import reduce.fz.com.reduce.util.ServiceBuilder;

/**
 * 故事回复评界面
 */
public class StoryReplyActivity extends BaseToolBarActivity {
    //回复列表
    private FrameLayout mReplyListContainer;
    //回复fragment
    private StoryReplyFragment storyReplyFragment;
    //回复内容编辑框
    private EditText mEtReplyContent;
    //回复button
    private TextView mTvReplyBtn;
    //storyid
    private Long storyid;

    private StoryService storyService = ServiceBuilder.builderStoryService(this);

    public void initData() {
        storyid = getIntent().getLongExtra("storyid", 0);
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setToolbarTitle("回复列表");
        setContentView(R.layout.activity_story_reply);
        initData();
        initWidget();
        initReplyList();
        initEditListener();
        initReplyBtnListener();
    }


    /**
     * 初始化并替换Fragment
     */
    public void initReplyList() {
        storyReplyFragment = storyReplyFragment.create(storyid);
        getSupportFragmentManager().beginTransaction().replace(R.id.reply_list_container, storyReplyFragment).commit();

    }

    /**
     * 初始化组件信息
     */
    public void initWidget() {
        mReplyListContainer = (FrameLayout) findViewById(R.id.reply_list_container);
        mEtReplyContent = (EditText) findViewById(R.id.et_reply_content);
        mTvReplyBtn = (TextView) findViewById(R.id.tv_reply_btn);
    }

    @Override
    public void initListener() {

    }

    public void initEditListener() {
        mEtReplyContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //获取焦点
                InputMethodUtil.reuestFocusAndShowInput(mEtReplyContent);
            }
        });
    }

    /**
     * 组装评论对象
     *
     * @return
     */
    public StoryReply combReply() {
        StoryReply reply = new StoryReply();
        reply.description = mEtReplyContent.getText().toString().trim();
        reply.storyid = storyid;
        reply.replytime = System.currentTimeMillis();
        User user = UserPreference.getUser();
        reply.user = user;
        return reply;
    }

    /**
     * 发送评论监听
     */

    private TextWatcher watcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (TextUtils.isEmpty(mEtReplyContent.getText().toString()))
                mTvReplyBtn.setTextColor(getResources().getColor(R.color.text_gray3));
            else
                mTvReplyBtn.setTextColor(getResources().getColor(R.color.text_blue));
        }
    };

    public void initReplyBtnListener() {
        mEtReplyContent.addTextChangedListener(watcher);
        mTvReplyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //清除焦点并隐藏输入法
                InputMethodUtil.clearFocus(mEtReplyContent);
                final StoryReply reply = combReply();
                final LoadingDialog loadingDialog = new LoadingDialog(StoryReplyActivity.this);
                loadingDialog.setMsg("提交中,请稍候!").show();
                storyService.replyStory(reply.storyid, reply.description, new AbResponseListener<HashMap>(new TypeReference<Result<HashMap>>() {
                }) {
                    @Override
                    public void error(ServiceException e) {
                    }
                    @Override
                    public void responseInfo(Result<HashMap> data) {
                        storyReplyFragment.addReply(reply);
                        mEtReplyContent.setText("");
                        loadingDialog.dismiss();
                        mTvReplyBtn.setTextColor(getResources().getColor(R.color.text_gray3));
                    }
                });
            }
        });

    }

}
